package com.csust.cg.mongodb.contorller;

import com.csust.cg.mongodb.domain.HttpStatus;
import com.csust.cg.mongodb.domain.Result;
import com.csust.cg.mongodb.service.RecentConnectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author chenguang
 */
@RestController
@RequestMapping("/api/recent_connect")
@ResponseBody
public class RecentConnectController {

    @Autowired
    private RecentConnectService recentConnectService;

    @PostMapping("/addRecentConnect")
    public Result addRecentConnect(@RequestParam("connectionId") String connectionId, @RequestParam("token") String token) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (StringUtils.isBlank(connectionId)) {
            return new Result("连接ID不能为空！", HttpStatus.BAD_REQUEST);
        }
        return recentConnectService.addRecentConnectByConnectionId(connectionId, token);
    }

    @PostMapping("/removeRecentConnects")
    public Result removeRecentConnects(@RequestParam("token") String token, @RequestParam("ids") ArrayList<Integer> ids) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (ids == null || ids.size() < 1) {
            return Result.error("连接信息不能为空！");
        }
        return recentConnectService.removeRecentConnects(token, ids);
    }

    @PostMapping("/removeAllRecentConnect")
    public Result removeAllRecentConnect(@RequestParam("token") String token) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        return recentConnectService.removeAllRecentConnect(token);
    }

    @GetMapping("/getAllRecentConnectList")
    public Result getAllRecentConnectList(@RequestParam("token") String token) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        return recentConnectService.getAllRecentConnectList(token);
    }
}
